import React from 'react';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as Action from "./actions";
import { HashRouter as Router, Route, Link } from 'react-router-dom'

class Footer extends React.Component {
    constructor(props) {
        super(props);
        var parm=JSON.parse(sessionStorage.getItem('userOpenid'));
    }

    render() {
        const keepTwoDecimal=(num)=>{
            var result = parseFloat(num);
            result = Math.round(num * 100) / 100;
            return result;
        }
        var link = !this.props.confirm 
        ? <Link to="/confirm">选好了</Link>
        : <Link to="/success" onClick={() => { this.props.actions.createOrder(sessionStorage,this.props.shop) }}>下单</Link>
        let num = 0, prcieC = 0;
        this.props.shop.forEach((item) => {
            prcieC += item.quantity * item.price;
            num += item.quantity;
        })
        return <Router>
            <footer>
                <div className="shopping">
                    <a className="shopIco" onClick={() => { this.props.change() }}>
                        {
                            num
                                ? [
                                    <img key="1" src="images/shoppingCar2.png" />,
                                    <i key="2">{num}</i>
                                ]
                                : <img src="images/shoppingCar.png" />
                        }
                    </a>
                    {prcieC ? <span className="prcieC">￥{keepTwoDecimal(prcieC)}</span> : <span>购物车是空的</span>}
                </div>
                {
                    num
                        ? <div className="submitBtn1">
                            {link}
                        </div>
                        :
                        <div className="submitBtn">
                            <a>选好了</a>
                        </div>
                }
            </footer>
        </Router>
    }
}
export default connect(
    ({ foodReducer }) => {
        return {
            shop: foodReducer.shop
        }
    },
    (dispatch) => {
        return {
            actions: bindActionCreators(Action, dispatch)
        }
    }
)(Footer)